from django.shortcuts import render

# Create your views here.


from rest_framework.viewsets import ViewSetMixin,GenericViewSet
from rest_framework.generics import ListAPIView,RetrieveAPIView
from rest_framework.mixins import ListModelMixin,RetrieveModelMixin
from .models import CourseCategory, Course,CourseChapter
from .serializer import CourseCategoryModelSerializer, CourseModelSerializer,CourseChapterModelSerializer
from rest_framework.filters import OrderingFilter,SearchFilter
from django_filters.rest_framework import DjangoFilterBackend
from .pagination import CommonPageNumberPagination

from .filter import  MyFilter,CourseFilterSet
# class CourseCategoryView(ViewSetMixin, ListAPIView,RetrieveModelMixin):
# class CourseCategoryView(ViewSetMixin, GenericAPIView,ListModelMixin,RetrieveModelMixin):
class CourseCategoryView(GenericViewSet, ListModelMixin):
    queryset = CourseCategory.objects.all()
    serializer_class = CourseCategoryModelSerializer



class CourseView(GenericViewSet, ListModelMixin,RetrieveModelMixin):
    queryset = Course.objects.all()
    serializer_class = CourseModelSerializer

    filter_backends=[OrderingFilter,DjangoFilterBackend]
    ordering_fields=['id','price','students']
    # 1 如果使用了DjangoFilterBackend过滤类，需要配套filter_fields来使用
    # filter_fields = ('course_category',)
    pagination_class = CommonPageNumberPagination


    # 2 如果使用了DjangoFilterBackend过滤类，也可以配套一个类来使用,在类中写要过滤的字段
    filter_class=CourseFilterSet



class CourseChapterView(GenericViewSet, ListModelMixin):
    queryset = CourseChapter.objects.all()
    serializer_class = CourseChapterModelSerializer
    # 配置一个过滤，以课程为过滤条件
    filter_backends = [DjangoFilterBackend,]
    filter_fields = ('course',)



class CourseSearchView(GenericViewSet, ListModelMixin):
    queryset = Course.objects.all()
    serializer_class = CourseModelSerializer

    filter_backends=[SearchFilter,]
    pagination_class = CommonPageNumberPagination
    search_fields=['name']

    # def list(self, request, *args, **kwargs):
    #
    #     response=super().list( request, *args, **kwargs) # 查到的只是实战课
    #     search=request.query_params.get('search')
    #     # 轻课表查询，序列化
    #     # 免费课表查询，序列化
    #
    #     data={'actual_course':response.data,'free':[],'lite_course':[]}
